package com.aaa.tmr.controller;

import com.aaa.tmr.entity.PageBean;
import com.aaa.tmr.entity.ReturnBean;
import com.aaa.tmr.entity.User;
import com.aaa.tmr.service.CommonFileService;
import com.aaa.tmr.service.UserService;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户信息表(User)表控制层
 *
 * @author makejava
 * @since 2021-03-19 11:35:46
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private HttpSession session;

    @Resource
    private CommonFileService commonFileService;

    /**
     * 跳转登录
     * @param model
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(Model model){
        model.addAttribute("errorInfo",session.getAttribute("errorInfo"));
        return "back/login";
    }

    /**
     *登陆方法
     * @param user
     * @return
     */
    @RequestMapping("login")
    public String login(User user,Model model){
        //收集用户信息
        UsernamePasswordToken usernamePasswordToken =
                new UsernamePasswordToken(user.getUserName(),
                        user.getPassword());
        //获取当前登录对象
        Subject subject = SecurityUtils.getSubject();
        try {
            //登陆
            subject.login(usernamePasswordToken);
            //获取session
            Session session = subject.getSession();
            //把用户信息放入session
            session.setAttribute("userInfo",subject.getPrincipal());
            //this.session.removeAttribute("errorInfo");
            return "redirect:/backIndex/toIndex";
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        session.setAttribute("errorInfo","用户名或密码错误");
        return "redirect:toLogin";  //区分forward
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }



    /**
     * 跳转部门分页列表页面
     * @return
     */
    @RequestMapping("toPage")
    public String toPage(){
        return "back/user-page";
    }


    /**
     * 分页查询方法
     * @param page
     * @param limit
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, User user){
        PageInfo pageInfo = userService.queryAll(page, limit, user);
        return  new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }
    /**
     * @param pageBean:
     * @param user:
     * @return ReturnBean
     * @create by: IvanZ
     * @description : 查询所有，并分页，并按条件查询
     * @create time: 2020/12/17 10:09
     */
    @ResponseBody
    @GetMapping("findAll")
    public ReturnBean findAll(PageBean pageBean, User user) {
        List<User> users = userService.queryAllByLimit(getOffset(pageBean), pageBean.getLimit(), user);
        long count = userService.getCount(user);
        ReturnBean returnBean = returnSuccess(users, count);
        return returnBean;
    }


    /**
     * 部门添加
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("insert")
    public Result  insert(User user){
        return  new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0, userService.insert(user));
    }

    /**
     * 部门更新
     * @param user
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public Result  update(User user){
        return  new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0, userService.update(user));
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteById")
    public Result deleteById(Integer userId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),0,
                this.userService.deleteById(userId));
    }



    /**
     * 通过主键删除数据
     *
     * @param userIds 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(String userIds) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),0,
                this.userService.batchDeleteById(userIds));
    }
    /**
     * 头像上传
     * @param headPic
     * @return
     */
    @ResponseBody
    @RequestMapping("uploadHeadPic")
    public Result uploadHeadPic(@RequestParam MultipartFile headPic){
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.commonFileService.uploadFile(headPic,"files/user/images"));
    }

}
